package GUI;
import CTRLayer.*;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AddSupplier extends JFrame {

	private Controller ctr;
	
	private JPanel contentPane;
	private JTextField txtID;
	private JTextField txtAddress;
	private JTextField txtCity;
	private JTextField txtZIP;
	private JTextField txtCountry;
	private JTextField txtName;
	private JTextField txtPhone;
	private JTextField txtEmail;
	
	private boolean errorP;
	private boolean errorZ;
	

	/**
	 * Create the frame.
	 */
	public AddSupplier() {
		setResizable(false);
		setTitle("Add supplier");
		
		ctr = new Controller();
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 185);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblID = new JLabel("ID:");
		lblID.setBounds(10, 14, 70, 14);
		contentPane.add(lblID);
		
		txtID = new JTextField();
		txtID.setColumns(10);
		txtID.setBounds(79, 11, 86, 20);
		contentPane.add(txtID);
		
		JLabel lblAddress = new JLabel("Address:");
		lblAddress.setBounds(262, 14, 70, 14);
		contentPane.add(lblAddress);
		
		txtAddress = new JTextField();
		txtAddress.setColumns(10);
		txtAddress.setBounds(342, 11, 86, 20);
		contentPane.add(txtAddress);
		
		txtCity = new JTextField();
		txtCity.setColumns(10);
		txtCity.setBounds(342, 36, 86, 20);
		contentPane.add(txtCity);
		
		txtZIP = new JTextField();
		txtZIP.setColumns(10);
		txtZIP.setBounds(342, 61, 86, 20);
		contentPane.add(txtZIP);
		
		txtCountry = new JTextField();
		txtCountry.setColumns(10);
		txtCountry.setBounds(342, 86, 86, 20);
		contentPane.add(txtCountry);
		
		JLabel lblCountry = new JLabel("Country:");
		lblCountry.setBounds(262, 89, 70, 14);
		contentPane.add(lblCountry);
		
		JLabel lblZIP = new JLabel("ZIP code:");
		lblZIP.setBounds(262, 64, 70, 14);
		contentPane.add(lblZIP);
		
		JLabel lblCity = new JLabel("City:");
		lblCity.setBounds(262, 39, 70, 14);
		contentPane.add(lblCity);
		
		txtName = new JTextField();
		txtName.setColumns(10);
		txtName.setBounds(79, 36, 86, 20);
		contentPane.add(txtName);
		
		JLabel lblName = new JLabel("Name:");
		lblName.setBounds(10, 39, 70, 14);
		contentPane.add(lblName);
		
		JLabel lblEmail = new JLabel("Email:");
		lblEmail.setBounds(10, 89, 70, 14);
		contentPane.add(lblEmail);
		
		JLabel lblPhone = new JLabel("Phone:");
		lblPhone.setBounds(10, 64, 70, 14);
		contentPane.add(lblPhone);
		
		txtPhone = new JTextField();
		txtPhone.setColumns(10);
		txtPhone.setBounds(79, 61, 86, 20);
		contentPane.add(txtPhone);
		
		txtEmail = new JTextField();
		txtEmail.setColumns(10);
		txtEmail.setBounds(79, 86, 86, 20);
		contentPane.add(txtEmail);
		
		JButton btnBack = new JButton("Back to previous");
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				dispose();
			}
		});
		btnBack.setBounds(10, 117, 118, 23);
		contentPane.add(btnBack);
		
		JButton btnAdd = new JButton("Add customer");
		btnAdd.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				addSupplierToDB(txtID.getText(), txtName.getText(), txtPhone.getText(), txtEmail.getText(), txtAddress.getText(), txtCity.getText(), txtZIP.getText(), txtCountry.getText());
			}
		});
		btnAdd.setBounds(313, 117, 118, 23);
		contentPane.add(btnAdd);
	}
	
	private void addSupplierToDB(String id, String name, String phone, String mail, String address, String city, String zip, String country)
	{
		if(id.isEmpty() || name.isEmpty() || phone.isEmpty() || mail.isEmpty() || address.isEmpty() || city.isEmpty() || zip.isEmpty() || country.isEmpty())
		{
			JOptionPane.showMessageDialog(null,
				    "All fields have to be filled.",
				    "Warning",
				    JOptionPane.WARNING_MESSAGE);
		}
		else
		{	
			errorP=false;
			try
			{
				Integer.valueOf(phone);
			}
			catch(Exception e)
			{
				JOptionPane.showMessageDialog(null,
						"The phone number should be a number!",
						"Warning",
						JOptionPane.WARNING_MESSAGE);
				errorP=true;
			}

			if(errorP!=true)
			{
				errorZ=false;
				try
				{
					Integer.valueOf(zip);
				}
				catch(Exception e)
				{
					JOptionPane.showMessageDialog(null,
							"The ZIP code should be a number!",
							"Warning",
							JOptionPane.WARNING_MESSAGE);
					errorZ=true;
				}

				if(errorZ!=true)
				{	
					if((ctr.getSupp(name)))
					{
						JOptionPane.showMessageDialog(null,
							"Supplier with the same name already exists.",
						   	"Warning",
						   	JOptionPane.WARNING_MESSAGE);
					}
					else
					{
						ctr.newSupp(id, name, phone, mail, address, city, zip, country);
						
						//table at customer window.addElement(/*???*/);
						
						dispose();
					
					}
				}
			}
		}
	}
}


